﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GBMS.DAL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DALFactory;
using KPIS.GERP.GBMS.IBLL;

namespace KPIS.GERP.GBMS.BLL
{
    public class ExpenseBLL : IExpenseBLL
    {
        private static readonly ISourceDAL src = DataAccess.CreateSource();
        private static readonly ISourceTypeDAL srcType = DataAccess.CreateSourceType();
        private static readonly IExpenseGroupDAL expGrpCtrl = DataAccess.CreateExpenseGroup();
        private static readonly IExpenseDAL dal = DataAccess.CreateExpense();

        public ExpenseBLL()
        {

        }

        public IList<TreeViewInfo> TreeView(IList<TreeViewInfo> tView, bool isRootNode, string expGrp)
        {
            IList<TreeViewInfo> treeView = new List<TreeViewInfo>();
            int inexpGrp = (expGrp.Length > 0) ? Convert.ToInt32(expGrp) : 0;

            tView = dal.TreeView(tView, isRootNode, new ExpenseInfo(0, "", "", "", "", null, new ExpenseGroupInfo(inexpGrp), null, null, null, null, null));

            return tView;
        }

        public string Insert(string code, string name, string descr, string expType, string parent
            , string expGrp, string expGrpReal, string IcmResourceType, string IcmResourceSeq, string GfmisCode)
        {
            string[] strParent;
            ExpenseInfo parentInfo = new ExpenseInfo();

            if (parent.Length > 0)
            {
                //strParent = parent.Split('-');
                //if(strParent[0].ToString() != "EXPENSEGROUP")
                parentInfo.Sequence = Convert.ToInt32(parent.ToString());
            }
            else
            {
                parentInfo = null;
            }

            Nullable<int> nIcmResourceSeq = null;
            if (IcmResourceSeq != null)
            {
                if (IcmResourceSeq.Length > 0)
                {
                    nIcmResourceSeq = Convert.ToInt32(IcmResourceSeq);
                }
            }

            ExpenseInfo expInfo = new ExpenseInfo(0, code, name, descr, "U", parentInfo
                , new ExpenseGroupInfo(Convert.ToInt32(expGrp))
                , new ExpenseGroupInfo(Convert.ToInt32(expGrpReal))
                , new UserRecordInfo(1, null)
                , IcmResourceType, nIcmResourceSeq, GfmisCode);

            return dal.Insert(expInfo).ToString();

        }

        public void Update(string seq, string code, string name, string descr, string expType
            , string parent, string expGrp, string expGrpReal, string IcmResourceType, string IcmResourceSeq, string GfmisCode)
        {
            string[] strParent;
            ExpenseInfo parentInfo = new ExpenseInfo();

            if (parent.Length > 0)
            {
                if (parent.IndexOf('-') > 0)
                {
                    strParent = parent.Split('-');
                    parentInfo.Sequence = Convert.ToInt32(strParent[1].ToString());
                }
                else parentInfo.Sequence = Convert.ToInt32(parent);
            }
            else
            {
                parentInfo = null;
            }

            Nullable<int> nIcmResourceSeq = null;
            if (IcmResourceSeq != null)
            {
                if (IcmResourceSeq.Length > 0)
                {
                    nIcmResourceSeq = Convert.ToInt32(IcmResourceSeq);
                }
            }

            ExpenseInfo expInfo = new ExpenseInfo(int.Parse(seq), code, name, descr, "", parentInfo
                , new ExpenseGroupInfo(Convert.ToInt32(expGrp))
                , new ExpenseGroupInfo(Convert.ToInt32(expGrpReal))
                , new UserRecordInfo(1, null)
                , IcmResourceType, nIcmResourceSeq, GfmisCode);

            int chkError = dal.Update(expInfo);

            if (chkError == 0)
            {
                // Error
            }
        }

        public void Delete(string seq)
        {
            int inSeq = (seq.Length > 0) ? Convert.ToInt32(seq) : 0;

            dal.Delete(inSeq);
        }

        public IList<ExpenseInfo> Search(string seq, string expType)
        {
            return Search(seq, "", "", "", expType, "", "", "");
        }

        public IList<ExpenseInfo> Search(string seq, string code, string name, string descr, string expType, string parent, string expGrp, string expGrpReal)
        {
            int inSeq = (seq.Length > 0) ? Convert.ToInt32(seq) : 0;

            ExpenseInfo parentInfo = new ExpenseInfo();

            if (parent != null)
            {
                parentInfo.Sequence = (parent.Length > 0) ? Convert.ToInt32(parent) : 0;
            }
            else
                parentInfo = null;

            int inexpGrp = (expGrp.Length > 0) ? Convert.ToInt32(expGrp) : 0;
            int inexpGrpReal = (expGrpReal.Length > 0) ? Convert.ToInt32(expGrpReal) : 0;

            ExpenseInfo expInfo = new ExpenseInfo(inSeq, code, name, descr, "", parentInfo
                , new ExpenseGroupInfo(Convert.ToInt32(inexpGrp)), new ExpenseGroupInfo(Convert.ToInt32(inexpGrpReal)), null, null, null, null);

            return dal.Search(expInfo);
        }

        public bool CheckDuplicateCode(string code)
        {
            return dal.CheckDuplicateCode(code);
        }
    }
}
 